import 'package:favorcate/core/model/meal_model.dart';
import 'package:favorcate/core/viewmodel/favor_view_model.dart';
import 'package:favorcate/ui/pages/detail/meal_detail.dart';
import 'package:favorcate/ui/shared/size_fit.dart';
import 'package:favorcate/ui/widgets/operation_item.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class JKMealItem extends StatelessWidget {

  final JKHomeMealModel _meal;

  JKMealItem(this._meal);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Card(
        margin: EdgeInsets.all(8.px),
        // 阴影，默认是1
        elevation: 4,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8.px)),
        child: Column(
          children: <Widget>[
            buildBasicInfo(context),
            buildOPerationInfo(context)
          ],
        ),
      ),
      onTap: () {
        Navigator.of(context).pushNamed(JKMealDetailScreen.routeName, arguments: _meal);
      },
    );
  }

  Widget buildBasicInfo(BuildContext context) {
    return Stack(
      children: <Widget>[
        ClipRRect(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(12.px),
            topRight: Radius.circular(12.px)
          ),
          child: Image.network(_meal.imageUrl, width: double.infinity, height: 250.px, fit: BoxFit.cover,),
        ),
        Positioned(
          right: 10.px,
          bottom: 10.px,
          child: Container(
            width: 250,
            padding: EdgeInsets.symmetric(horizontal: 10.px, vertical: 5.px),
            decoration: BoxDecoration(
              color: Colors.black54,
              borderRadius: BorderRadius.circular(6.px)
            ),
            child: Text(_meal.title, style: Theme.of(context).textTheme.headline4.copyWith(
              color: Colors.white
            ),),
          ),
        )
      ],
    );

  }

  Widget buildOPerationInfo(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(16.px),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          JKOPerationItem(Icon(Icons.schedule), '${_meal.duration}分钟'),
          JKOPerationItem(Icon(Icons.restaurant), '${_meal.complexityStr}'),
          buildFavorItem(),
        ],
      ),
    );
  }

  Widget buildFavorItem() {
    return Consumer<JKFavorViewModel>(
      builder: (ctx, favorVM, child) {
        // 判断是否是收藏状态
        // 图片
        final iconData = favorVM.isFavor(_meal) ? Icons.favorite : Icons.favorite_border;
        // 颜色
        final iconolor = favorVM.isFavor(_meal) ? Colors.red : Colors.black;
        // 文字
        final title = favorVM.isFavor(_meal) ? '已收藏' : '未收藏';
        // 字体颜色
        final titleColor = favorVM.isFavor(_meal) ? Colors.red : Colors.black;
        return GestureDetector(
          child: JKOPerationItem(Icon(iconData, color: iconolor,), title, textColor: titleColor,),
          onTap: () {
            favorVM.handleMeal(_meal);
          },
        );
      },
    );
  }
}
